package org.example.system.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.News;
import org.example.system.service.NewsService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 新闻控制层
 */
@RestController
@RequestMapping("/app/news")
@Slf4j
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 按条件分页查询
     * @param news
     * @return
     */
    @GetMapping("/list")
    public R list(News news){
        IPage<News> page = newsService.selectPage(news);
        log.info("------------ NewsController | list | selectPage | return : {} ", JSON.toJSONString(page));
        return R.ok(page);
    }

    /**
     * 根据id查询新闻数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable("id") Long id){
        log.info("--------- NewsController | getInfo | input : {}", JSON.toJSON(id));
        return R.ok(newsService.getOne(id));
    }
}
